package com.example.pei.ithinklearnt.adapter;


import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.pei.ithinklearnt.AppApplication;
import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.adapter.base.BaseViewHolder;
import com.example.pei.ithinklearnt.adapter.base.OnRecyclerViewListener;
import com.example.pei.ithinklearnt.bean.UserInfo;
import com.example.pei.ithinklearnt.listener.QueryUserListener;
import com.example.pei.ithinklearnt.model.UserModel;
import com.example.pei.ithinklearnt.view.activity.UserInfoActivity0;
import com.makeramen.roundedimageview.RoundedImageView;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.v3.exception.BmobException;

import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.displayImage;
import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.getPhotoImageOption;
import static com.example.pei.ithinklearnt.utils.MyUtils.getSendTime;

/**
 * 接收到的文本类型
 */
public class ReceiveTextHolder extends BaseViewHolder {

    @BindView(R.id.iv_avatar)
    RoundedImageView iv_avatar;

    @BindView(R.id.tv_time)
    TextView tv_time;

    @BindView(R.id.tv_message)
    TextView tv_message;

    public ReceiveTextHolder(Context context, ViewGroup root, OnRecyclerViewListener onRecyclerViewListener) {
        super(context, root, R.layout.item_chat_received_message,onRecyclerViewListener);
    }

    @OnClick({R.id.iv_avatar})
    public void onAvatarClick(View view) {

    }

    @Override
    public void bindData(Object o) {
        final BmobIMMessage message = (BmobIMMessage)o;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format(message.getCreateTime());
        tv_time.setText(getSendTime(time ));
        final BmobIMUserInfo info = message.getBmobIMUserInfo();
        displayImage(AppApplication.getContext(), iv_avatar, info != null ? info.getAvatar():null, getPhotoImageOption());
        if(info==null)return;
        if(info.getAvatar().equals("")||info.getAvatar()==null){
            iv_avatar.setImageResource(R.drawable.default_head);
        }
        String content =  message.getContent();
        tv_message.setText(content);
        //点击用户头像
        iv_avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel.getInstance().queryUserInfo(info.getUserId(), new QueryUserListener() {
                    @Override
                    public void done(UserInfo s, BmobException e) {
                        if(e==null){
                            Bundle bundle=new Bundle();
                            bundle.putSerializable("u",s);
                            startActivity(UserInfoActivity0.class,bundle);
                        }
                    }
                });

            }
        });
        //点击文字信息
        tv_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // toast("点击"+message.getContent());
                if(onRecyclerViewListener!=null){
                    onRecyclerViewListener.onItemClick(v,getAdapterPosition());
                }
            }
        });
        //长按文字信息
        tv_message.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onRecyclerViewListener != null) {
                    onRecyclerViewListener.onItemLongClick(v,getAdapterPosition());
                }
                return true;
            }
        });
    }

    public void showTime(boolean isShow) {
        tv_time.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }
}